package com.sky.controller.user;

import com.sky.dao.CancelDTO;
import com.sky.dao.HistoryOrdersPageDTO;
import com.sky.dao.OrderDTO;
import com.sky.dao.OrdersPaymentDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "用户-订单")
@RequestMapping("/user/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("用戶下單")
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrderDTO orderDTO) {
        OrderSubmitVO orderSubmitVO = orderService.submit(orderDTO);
        return Result.success(orderSubmitVO);
    }

    @ApiOperation("歷史訂單查詢")
    @GetMapping("/historyOrders")
    public Result<PageResult> historyOrders(HistoryOrdersPageDTO historyOrdersPageDTO) {

        PageResult pageResult = orderService.historyOrders(historyOrdersPageDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("查詢訂單詳情")
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> orderDetail(@PathVariable("id") Long id) {
        OrderVO orderVO = orderService.orderDetail(id);
        return Result.success(orderVO);
    }

    @ApiOperation("訂單支付")
    @PutMapping("/payment")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {

        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成預支付交易單:{}", orderPaymentVO);

        return Result.success(orderPaymentVO);
    }

    @ApiOperation("取消訂單")
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable("id") Long id) {
        CancelDTO cancelDTO = new CancelDTO();
        cancelDTO.setId(id);
        orderService.cancel(cancelDTO);
        return Result.success();
    }

    @ApiOperation("再來一單")
    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable("id") Long id) {
        orderService.repetition(id);
        return Result.success();
    }
    }
